******************************************************************************************
* Do-file name:	an_dec_wage_01.do                                                        
* Task:         run regressions on wage decomposition, municipality level                
* Last change:  04.04.2023                                                                                                      
* Notes:                                                                                 
/*
This file contains the code used to generate the results presented in the following tables and figures:
- Figure 1, Panel B: 
		-- Regional wage effect: g_y_nat_s1

- Table 2:
		-- Regional wage effect: g_y_nat_s1
		-- Compositional effect: dec_wage_2_c_s1
		-- Inflows: dec_wage_2_s1
		-- Outflows: dec_wage_3_s1

- Table A.4:
		-- Regional wage effect: g_y_nat_s1
*/
******************************************************************************************



******************************************************************************************
*** program setup
******************************************************************************************

version 14.2
clear all
macro drop _all
set linesize 90
set more off
* set trace on
discard
set seed 123456789
*set matsize 2000



******************************************************************************************
*** load working dataset
******************************************************************************************

foreach x of numlist 1 {
use "data/wage_region_s`x'.dta", clear



******************************************************************************************
*** set globals
******************************************************************************************

* 1) 
global sample_wage_region_s1 "All full-time workers (natives)"
global weight_s1 "emp_nat_90*weight_matching"

*** use different czech shocks
foreach year of numlist 1987/1990 1992/1995 {
global czech_shock_`year' "czshare_92_90_predic_13"
	}

global czech_shock_1991   "czshare_91_90_predic_13"



******************************************************************************************
*** estimate wage change
******************************************************************************************

*** create global for loop
global wage_nat_dcomp  "g_y_nat  dec_wage_2_c  dec_wage_2  dec_wage_3"


*** run different outcome variables
foreach var of global wage_nat_dcomp {

foreach y of numlist 1987/1995 {

*** 1) basic model: full sample
reg `var'  ${czech_shock_`y'}  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_s`x'}], cluster(ao_kreis_imp)
est store `var'`y'


*** check sample
dis "Sample: ${sample_wage_region_s`x'}"
dis "Outcome Variable: `var'"
dis "Estimation weight: ${weight_s`x'}" 
dis "Year = `y'"
dis "Sample restriction version: 16-65, full-time employed natives (pers_gr: 101 & 102)"
count if e(sample)


********* bootstrap standard errors ***************************************************************
preserve
keep if e(sample)
drop czshare_92_90_predic_13
forval bs = 1/500 {
	di "bootstrap sample `bs'"
	merge m:1 ao_gem_imp using "data/bootstrap/bs_wild_first_92_`bs'.dta", nogenerate
	reg `var'  czshare_92_90_predic_13  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_s`x'}]
	predict res  if e(sample), res
	predict xb   if e(sample), xb
	gen y = xb + wild * res
	qui reg y  czshare_92_90_predic_13  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_s`x'}]
	scalar coef`bs' = _b[czshare_92_90_predic_13]
	drop y res xb czshare_92_90_predic_13 wild
}
gen bs = . 
gen coef_`var'_`y' = .
forval bs = 1/500 {
	replace bs = `bs' in `bs'
	replace coef_`var'_`y' = coef`bs' in `bs'
}
keep bs coef_`var'_`y'
keep if bs != .
save "data/bootstrap/wage_decomp/bs_`var'_`y'.dta", replace
sum coef_`var'_`y'		// std. dev. = std. error
restore

***************************************************************************************************

********* bootstrap standard errors 1991 **********************************************************
if `y' == 1991 {
preserve
keep if e(sample)
drop czshare_92_90_predic_13
forval bs = 1/500 {
	di "bootstrap sample `bs'"
	merge m:1 ao_gem_imp using "data/bootstrap/bs_wild_first_91_`bs'.dta", nogenerate
	reg `var'  czshare_91_90_predic_13  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_s`x'}]
	predict res  if e(sample), res
	predict xb   if e(sample), xb
	gen y = xb + wild * res
	qui reg y  czshare_91_90_predic_13  if year == `y' & (border_imp_13 == 1 | control_imp == 1) [pweight=${weight_s`x'}]
	scalar coef`bs' = _b[czshare_91_90_predic_13]
	drop y res xb czshare_91_90_predic_13 wild
}
gen bs = . 
gen coef_`var'_`y' = .
forval bs = 1/500 {
	replace bs = `bs' in `bs'
	replace coef_`var'_`y' = coef`bs' in `bs'
}
keep bs coef_`var'_`y'
keep if bs != .
save "data/bootstrap/wage_decomp\1991_cz_shock/bs_`var'_`y'.dta", replace
sum coef_`var'_`y'		// std. dev. = std. error
restore
}
***************************************************************************************************
	}


*** create tables
#delimit ;
global estout_wage "cells(b(star fmt(%9.3f) vacant(-)) se(fmt(%9.3f) par)) 
stats(N N_clust r2_a, labels("Observations" "Clusters" "Adjusted R2") layout(@ @ @) fmt(%9.0fc %9.0fc %9.3f))
starlevels(* 0.1 ** 0.05 *** 0.01) varwidth(30)
varlabels(_cons "Constant" czshare_92_90_predic_13 "Share Czechs (92-90, pred.)" czshare_91_90_predic_13 "Share Czechs (91-90, pred.)") label
mlabels("Wage 87-90" "Wage 88-90" "Wage 89-90" "Wage 91-90" "Wage 92-90" "Wage 93-90" "Wage 94-90" "Wage 95-90")
prehead(@title) posthead() postfoot(@note) nonumbers collabels(none) style(tab)";
#delimit cr


** Table: Wage
sum obs_1990
local obs_1990 = r(mean)

estout  `var'1987  `var'1988  `var'1989  `var'1991  `var'1992  `var'1993  `var'1994  `var'1995 ///
using "tables\wage/tab_`var'_s`x'.txt", $estout_wage replace ///	
title(Wage Decomposition: `var' (1987-1995): Base, Sample: ${sample_wage_region_s`x'}) ///
note(Notes: Regional approach. * p<0.1, ** p<0.05, *** p<0.01. Obs. 1990: `obs_1990'. ///
Data Source: German Social Security Records, border districts and matched control districts, 1987-1995.)
	}
	}


******************************************************************************************
*** end
******************************************************************************************

exit


*========================================================================================*
Comments:
- unique identifier: vsnr_ano year
